package com.jusyl.cristo.mail;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class MailItfAch implements MailItf {

	@Override
	public void basicSendMail() {
		
        Properties props = new Properties();//key value:配置参数。真正发送邮件时再配置
        props.setProperty("mail.transport.protocol", "smtp");//指定邮件发送的协议，参数是规范规定的
        props.setProperty("mail.host", "smtp.126.com");//指定发件服务器的地址，参数是规范规定的
//        props.setProperty("mail.debug", "true");//邮件发送的调试模式，参数是规范规定的
        props.setProperty("mail.smtp.auth", "true");//请求服务器进行身份认证。参数与具体的JavaMail实现有关
        
        Session session = Session.getInstance(props);//发送邮件时使用的环境配置
        session.setDebug(true);
        MimeMessage message = new MimeMessage(session);
        
        
        try {
	        //设置邮件的头
	        message.setFrom(new InternetAddress("flxqe@126.com"));
	        message.addRecipients(Message.RecipientType.TO, "flxqe@163.com");
	        message.addRecipients(Message.RecipientType.CC, "flxqe@126.com");
	        message.setSubject("This is second message");
	        //设置正文
	        //message.setContent("<h1>hello</h1>", "text/html");
	        //message.setText("<h1>Happy new year !!!</h1>");//纯文本
	        message.setText("This is Umbrella Corporation.It's close to Chinese Spring Festival.Happy new year !!!");
	        message.addHeaderLine("Information for test.");
	        message.saveChanges();
	        
	        
	        
	        //发送邮件
	        Transport ts = session.getTransport();
	        ts.connect("flxqe@126.com", "hjhbyuan1211");       // 密码为授权码不是邮箱的登录密码
	        ts.sendMessage(message, message.getAllRecipients());//对象，用实例方法}
	        ts.close();
        }catch(Exception e) {
        	e.printStackTrace();
        }
		
	}
	
	
	public static void main(String arg[]){
		MailItf m = new MailItfAch();
		m.basicSendMail();
	}

}
